package com.example.kuaizu.aop;

import com.example.kuaizu.exception.SendMassageExcepTion;
import com.example.kuaizu.mapper.SubscribeMassageMapper;
import kuaizu.common.constp.WxMassageConst;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


/**
 * @author: wangmx15
 * @date: 2025/8/30
 **/
@Aspect
@Component
@Log4j2
@RequiredArgsConstructor
public class ExceptionAspect {

    private final SubscribeMassageMapper subscribeMassageMapper;

    // 异常通知：目标方法抛出异常时执行
    @AfterThrowing(pointcut = "execution(void com.example.kuaizu.mq.MQBuils.*Queues(..))", throwing = "e")
    public void afterThrowingAdvice(JoinPoint joinPoint, SendMassageExcepTion e) {
        if(WxMassageConst.wxError.containsKey(e.getSendMassageExcpTionClass().getErrcode())){
            log.error("【异常通知】{}方法抛出异常：{}", joinPoint.getSignature().getName(), WxMassageConst.wxError.containsKey(e.getSendMassageExcpTionClass().getErrcode())+e.getSendMassageExcpTionClass().getErrmsg());
            return ;
        }
        //mq异常 方式mq一直重试
        String methodName = joinPoint.getSignature().getName();
        log.error("【异常通知】{}方法抛出异常：{}", methodName, e.getMessage());
        //将异常数据保存到数据库
        subscribeMassageMapper.insertException(e.getCode(), e.getMessage(),-1);
        log.error("【异常通知】保存异常数据成功");
    }
}
